// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2025 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.15.11
package sys

import (
	"context"
	"fmt"
	"hotgo/internal/consts"
	"hotgo/internal/dao"
	"hotgo/internal/library/hgorm/handler"
	"hotgo/internal/model"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
)

type sSysUserModule struct{}

func NewSysUserModule() *sSysUserModule {
	return &sSysUserModule{}
}

func init() {
	service.RegisterSysUserModule(NewSysUserModule())
}

// Model 用户模块ORM模型
func (s *sSysUserModule) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
	return handler.Model(dao.AdminMember.Ctx(ctx), option...)
}

// List 获取用户模块列表
func (s *sSysUserModule) List(ctx context.Context, in *sysin.UserModuleListInp) (list []*sysin.UserModuleListModel, totalCount int, err error) {
	mod := s.Model(ctx).As("hg_admin_member")

	// 字段过滤
	mod = mod.FieldsPrefix("hg_admin_member", sysin.UserModuleListModel{}).
		LeftJoin("hg_admin_member AS parent ON hg_admin_member.pid = parent.id").
		Fields("parent.real_name AS parent_real_name").
		Fields("parent.nickname AS parent_nickname")

	mod = mod.WhereIn(
		dao.AdminMember.Columns().RoleId,
		//consts.RoleAllUser, TODO 临时注释
		consts.RoleAllUser2,
	)

	// 查询ID
	if in.ID > 0 {
		mod = mod.Where(dao.AdminMember.Columns().Id, in.ID)
	}

	// 查询微信昵称
	if in.Nickname != "" {
		mod = mod.WhereLike(dao.AdminMember.Columns().Nickname, "%"+in.Nickname+"%")
	}

	// 查询手机号
	if in.Mobile != "" {
		mod = mod.WhereLike(dao.AdminMember.Columns().Mobile, "%"+in.Mobile+"%")
	}

	// 查询用户角色
	if in.RoleId > 0 {
		mod = mod.Where(dao.AdminMember.Columns().RoleId, in.RoleId)
	}

	// 查询用户状态
	if in.MemberStatus > 0 {
		mod = mod.Where(dao.AdminMember.Columns().MemberStatus, in.MemberStatus)
	}

	// 查询会员类型
	if in.MemberType > 0 {
		mod = mod.Where(dao.AdminMember.Columns().MemberType, in.MemberType)
	}

	// 查询用户标签
	if in.Label != "" {
		mod = mod.Where(fmt.Sprintf(`JSON_CONTAINS(%s,json_array('%v'))`, dao.AdminMember.Table()+"."+dao.AdminMember.Columns().Label, in.Label))
	}

	// 查询渠道来源
	if in.Source > 0 {
		mod = mod.Where(dao.AdminMember.Columns().Source, in.Source)
	}

	// 分页
	mod = mod.Page(in.Page, in.PerPage)

	// 排序
	mod = mod.OrderDesc(dao.AdminMember.Columns().Id)

	// 查询数据
	if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
		err = gerror.Wrap(err, "获取用户模块列表失败，请稍后重试！")
		return
	}
	return
}

// Status 更新用户模块状态
func (s *sSysUserModule) Status(ctx context.Context, in *sysin.UserModuleStatusInp) (err error) {
	if _, err = s.Model(ctx).WherePri(in.Id).Data(g.Map{
		dao.AdminMember.Columns().Status: in.Status,
	}).Update(); err != nil {
		err = gerror.Wrap(err, "更新用户模块状态失败，请稍后重试！")
		return
	}
	return
}

// GetByOpenID 根据OpenID获取用户信息
func (this *sSysUserModule) GetByOpenID(ctx context.Context, openid string) (user *model.Identity, err error) {
	err = dao.AdminMember.Ctx(ctx).Where(dao.AdminMember.Columns().OpenId, openid).Scan(&user)
	return
}
